package com.ssm.service.impl;

import com.ssm.dao.UserDao;
import com.ssm.entity.User;
import com.ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by bai on 2017/7/26.
 */
@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired(required = false)
    private UserDao userDao;

    //登陆验证
    public User checkLogin(String username, String password) {
        //根据用户名实例化用户对象
        User user = userDao.queryByName(username);
        if (user != null && user.getUserPwd().equals(password)) {
            return user;
        }
        return null;
    }

    //注册验证，验证用户名是否已经注册过
    public boolean checkRegister(String username) {

        User user = userDao.queryByName(username);
        if(user != null && user.getUsername().equals(username)){

            //用户名已存在
            return false;
        }

        //用户名未注册
        return true;
    }

    //注册验证通过后，添加用户
    //或用于管理员添加用户
    public User addUser(User user){

        userDao.addUser(user.getUsername(),user.getUserPwd());
        return user;
    }


    //显示当前所有用户信息
    public List<User> queryAll() {

        List<User> allUsers = userDao.queryAll();

        return allUsers;
    }

    //管理员删除用户
    public void deleteUser(int userId) {

        User user = userDao.queryById(userId);
        if (user != null) {
            userDao.deleteUser(userId);
        }

    }

    //管理员更改用户信息
    public User updateUser(User user) {

        userDao.updateUser(user.getId(),user.getUsername(),user.getUserPwd());

        return user;
    }

    public User queryByUsername(String username) {
        User user = userDao.queryByName(username);
        return user;
    }

    //根据关键字查询符合条件的用户
    public List<User> queryByKeyName(String keyName) {
        return userDao.queryByKeyName(keyName);
    }


}
